package cn.cfoodc.sharding.controller;

import cn.cfoodc.sharding.domain.TComplex;
import cn.cfoodc.sharding.domain.TOrderItem;
import cn.cfoodc.sharding.service.TComplexService;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


/**
 * 按照createDatetime日期分表
 */
@RestController
public class TComplexController {

    private TComplexService tComplexService;
    private SnowflakeKeyGenerateAlgorithm NEXT_ID ;
    @Autowired
    public TComplexController(TComplexService tComplexService,SnowflakeKeyGenerateAlgorithm snowflakeKeyGenerateAlgorithm){
        this.tComplexService=tComplexService;
        this.NEXT_ID=snowflakeKeyGenerateAlgorithm;
    }


    //插入21年和22年的1-12月份数据
    @PostMapping("/addTComplex")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int insert( @RequestParam("createTime") Timestamp createTime ){
        //为了演示，按照yml设定最大时间
        Timestamp bs = Timestamp.valueOf("2022-12-31 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(createTime);
        Long userId =  (Long)NEXT_ID.generateKey();
        for (int i = 0; i < 24; i++) {
            c.add( Calendar.MONTH , 1 ); // c每次加一个月
            Timestamp time1 = new Timestamp(c.getTimeInMillis());
            if(time1.after(bs)){ //time1时间在bs时间后面
                break;
            }
            TComplex t = new TComplex(
                    (Long)NEXT_ID.generateKey(),userId, UUID.randomUUID().toString(),UUID.randomUUID().toString(),
                    time1,
                    time1,
                    time1
            );
            tComplexService.insertOne(t);
        }
        return  1;
    }

    //按照user_id定位到库，在当前库执行多表查询
    @GetMapping("/getOneComplexAndArgs_1")
    public TComplex getOne(@RequestParam("userId") Long userId , @RequestParam("id") Long id){
        TComplex complex = tComplexService.selectByPrimaryKey(userId , id);
        return complex ;
    }


    //按照user_id定位到库，按照createDatetime命中一个表，
    @GetMapping("/getOneComplexAndArgs_2")
    public TComplex getOne(@RequestParam("userId") Long userId , @RequestParam("createDatetime") String createDatetime){
        TComplex complex = tComplexService.selectByUseridAndCreatedate(userId , createDatetime);
        return complex ;
    }

    //按照创建时间全库全表查询，
    @GetMapping("/getAllTComplexAndArgs_3")
    public List<TComplex> selectAll( @RequestParam("benginCreateDatetime") String benginCreateDatetime , @RequestParam("endCreateDatetime") String endCreateDatetime){
        return tComplexService.selectAll(benginCreateDatetime , endCreateDatetime);
    }

    //删除数据
    @PostMapping("/deleteTComplex")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int delete( @RequestParam("id") Long id , @RequestParam("createDatetime") Timestamp createDatetime){
        return tComplexService.deleteByPrimaryKey(id , createDatetime);
    }

    //更新数据
    @PostMapping("/updateTComplex")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int update(@ModelAttribute TComplex tComplex){
        tComplexService.updateByPrimaryKey(tComplex);
        return 1;
    }
}
